/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.report.userreport.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.report.userreport.entity.UserReport;
import cn.apestar.modules.report.userreport.service.UserReportService;

/**
 * 用户收徒报表管理Controller
 * @author yudw
 * @version 2016-10-10
 */
@Controller
@RequestMapping(value = "${adminPath}/userreport/userReport")
public class UserReportController extends BaseController {

    @Autowired
    private UserReportService userReportService;

    @ModelAttribute
    public UserReport get(@RequestParam(required = false) String id) {
        UserReport entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = userReportService.get(id);
        }
        if (entity == null) {
            entity = new UserReport();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(UserReport userReport, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<UserReport> page = userReportService.findPage(new Page<UserReport>(request, response), userReport);

        model.addAttribute("page", page);
        return "modules/report/userreport/userReportList";
    }

    @RequestMapping(value = "form")
    public String form(UserReport userReport, Model model) {
        model.addAttribute("userReport", userReport);
        return "modules/report/userreport/userReportForm";
    }

    @RequestMapping(value = "save")
    public String save(UserReport userReport, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, userReport)) {
            return form(userReport, model);
        }
        userReportService.save(userReport);
        addMessage(redirectAttributes, "保存用户收徒报表管理成功");
        return "redirect:" + Global.getAdminPath() + "/userreport/userReport/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(UserReport userReport, RedirectAttributes redirectAttributes) {
        userReportService.delete(userReport);
        addMessage(redirectAttributes, "删除用户收徒报表管理成功");
        return "redirect:" + Global.getAdminPath() + "/userreport/userReport/?repage";
    }

}